package com.swjd.utpchat;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Scanner;

public class SendSocket implements Runnable {

	private DatagramSocket socket;
	private int sendPort;

	public SendSocket(DatagramSocket socket, int sendPort) {
		this.socket = socket;
		this.sendPort = sendPort;
	}

	@Override
	public void run() {
		Scanner sc = new Scanner(System.in);

		while (true) {
			try {
				System.out.println("请输入您要发送的数据：");
				String nextLine = sc.nextLine();
				byte[] bytes = nextLine.getBytes();
				// 装箱
				DatagramPacket packet = new DatagramPacket(bytes, bytes.length, InetAddress.getByName("127.0.0.1"),
						sendPort);
				// 发送
				socket.send(packet);
			} catch (Exception e) {

				e.printStackTrace();
			}
		}

	}

}
